package com.example.bottom_tab;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;

import com.example.bottom_tab.tab.tabView;
import com.example.bottom_tab.viewPagerAdapter.MyAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    tabView tab;
    ViewPager viewPager;
    List<Fragment> list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = findViewById(R.id.vp);
        tab =findViewById(R.id.tabV);
        initTab(tab);
        initViewpage(viewPager);
        tab.setOnClickBottomListener(new tabView.OnClickBottomListener() {
            @Override
            public void clickView(View v, int position) {
                viewPager.setCurrentItem(position);

            }
        }
    );

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tab.setSelectorPosition(position);
                System.out.println(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
    public void initTab(tabView tab){
        String[] texts = new String[]{"测试" , "测试1" , "测试2" , "测试3"};//这个是底部的文字
        int[] icons = new int[]{R.mipmap.ic_launcher , R.mipmap.ic_launcher ,
                R.mipmap.ic_launcher , R.mipmap.ic_launcher};//这个是底部未选中图标
        int[] selectorIcons = new int[]{R.mipmap.ic_launcher_round , R.mipmap.ic_launcher_round ,
                R.mipmap.ic_launcher_round , R.mipmap.ic_launcher_round};//这个是底部选中的图标
        tab.addIconAndText(icons , selectorIcons , texts);
    }
    public void initViewpage(ViewPager viewPager){
        list.add(new test1_fragmet());
        list.add(new test1_fragmet());
        list.add(new test1_fragmet());
        list.add(new test1_fragmet());
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager(), list));
    }
}